﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web.UI;
using Org.Eurekaa.DNJ.Settings;

namespace Org.Eurekaa.DNJ.UI.DNJSettings
{
    [TypeConverter(typeof(ExpandableObjectConverter))]
    [Serializable()]
    public class DNJDatePickerEvents : GenericSettingsObject
    {

        [Description("Can be a function that takes an input field and current datepicker instance and returns an options object to update the datepicker with. It is called just before the datepicker is displayed.")]
        [DefaultValue(""), NotifyParentProperty(true), RefreshProperties(RefreshProperties.Repaint)]
        [PersistenceMode(PersistenceMode.InnerProperty), DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public UIEventFnInput BeforeShow
        {
            get { return Get<UIEventFnInput>("beforeShow"); }
            set { this["beforeShow"] = value; }
        }

        [Description("The function takes a date as a parameter and must return an array with [0] equal to true/false indicating whether or not this date is selectable, [1] equal to a CSS class name(s) or '' for the default presentation and [2] an optional popup tooltip for this date. It is called for each day in the datepicker before is it displayed.")]
        [DefaultValue(""), NotifyParentProperty(true), RefreshProperties(RefreshProperties.Repaint)]
        [PersistenceMode(PersistenceMode.InnerProperty), DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public UIEventFnDate BeforeShowDay
        {
            get { return Get<UIEventFnDate>("beforeShowDay"); }
            set { this["beforeShowDay"] = value; }
        }

        [Description("Allows you to define your own event when the datepicker moves to a new month and/or year. The function receives the selected year, month (1-12), and the datepicker instance as parameters. this refers to the associated input field.")]
        [DefaultValue(""), NotifyParentProperty(true), RefreshProperties(RefreshProperties.Repaint)]
        [PersistenceMode(PersistenceMode.InnerProperty), DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public UIEventFnYMI OnChangeMonthYear
        {
            get { return Get<UIEventFnYMI>("onChangeMonthYear"); }
            set { this["onChangeMonthYear"] = value; }
        }

        [Description("Allows you to define your own event when the datepicker is closed, whether or not a date is selected. The function receives the selected date as text and the datepicker instance as parameters. this refers to the associated input field.")]
        [DefaultValue(""), NotifyParentProperty(true), RefreshProperties(RefreshProperties.Repaint)]
        [PersistenceMode(PersistenceMode.InnerProperty), DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public UIEventFnDtI OnClose
        {
            get { return Get<UIEventFnDtI>("onClose"); }
            set { this["onClose"] = value; }
        }

        [Description("Allows you to define your own event when the datepicker is selected. The function receives the selected date as text and the datepicker instance as parameters. this refers to the associated input field.")]
        [DefaultValue(""), NotifyParentProperty(true), RefreshProperties(RefreshProperties.Repaint)]
        [PersistenceMode(PersistenceMode.InnerProperty), DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public UIEventFnDtI OnSelect
        {
            get { return Get<UIEventFnDtI>("onSelect"); }
            set { this["onSelect"] = value; }
        }

        public DNJDatePickerEvents()
        {
            this["beforeShow"] = new UIEventFnInput();
            this["beforeShowDay"] = new UIEventFnDate();
            this["onChangeMonthYear"] = new UIEventFnYMI();
            this["onClose"] = new UIEventFnDtI();
            this["onSelect"] = new UIEventFnDtI();
        }

        public string GetDefaultEventsScript()
        {
            String evtScript = String.Empty;
            foreach (UIEventFunction evt in Settings.Values)
            {
                if (!string.IsNullOrEmpty(evt.Name))
                {
                    evtScript += "if (!window." + evt.Name.Trim() + "){" + evt.Name.Trim() + " = new Function(\"return dnjDefaultEvent('" + evt.Name.Trim() + "')\");}";
                }
            }

            return evtScript;
        }
        public override string ToString()
        {
            return base.ToString();
        }


    }

}
